package servlets;

import beans.Employee;
import service.EmployeeService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/updateProfileServlet")
public class updateProfileServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // Check if user is logged in
        HttpSession session = request.getSession();
        Employee employee = (Employee) session.getAttribute("user");
        String role = (String) session.getAttribute("role");

        if (employee == null || (!"employee".equals(role) && !"finance".equals(role) && !"driverLeader".equals(role))) {
            response.sendRedirect("login.jsp");
            return;
        }

        // Get form parameters
        String employeeName = request.getParameter("employeeName");
        String password = request.getParameter("password");
        String position = request.getParameter("position");
        String email = request.getParameter("email");
        String phoneNumber = request.getParameter("phoneNumber");
        String gender = request.getParameter("gender");

        // Validate required fields
        if (employeeName == null || employeeName.trim().isEmpty() ||
                phoneNumber == null || phoneNumber.trim().isEmpty()) {
            request.setAttribute("error", "姓名和电话不能为空");
            request.getRequestDispatcher("Profile.jsp").forward(request, response);
            return;
        }

        if (!phoneNumber.matches("[0-9]{11}")) {
            request.setAttribute("error", "请输入正确的11位手机号码");
            request.getRequestDispatcher("Profile.jsp").forward(request, response);
            return;
        }

        try {
            // Update employee information
            employee.setEmployeeName(employeeName);
            if (password != null && !password.trim().isEmpty()) {
                employee.setPassword(password);
            }
            employee.setPosition(position);
            employee.setEmail(email != null ? email : "");
            employee.setPhoneNumber(phoneNumber);
            employee.setGender(gender);

            // Save to database
            EmployeeService employeeService = new EmployeeService();
            int success = employeeService.updateEmployee(employee);

            if (success==1) {
                // Update session with new data
                session.setAttribute("user", employee);
                response.sendRedirect("Profile.jsp");
            } else {
                request.setAttribute("error", "个人信息更新失败");
                request.getRequestDispatcher("Profile.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", "系统错误: " + e.getMessage());
            request.getRequestDispatcher("Profile.jsp").forward(request, response);
        }
    }
}